<?php
 /******************************************************************************
 *   GunCMS is an avid supporter of open source software. This is the appropriate 
 *   option if you are creating an open source application with a license 
 *   compatible with the GNU GPL license v3.
 *   For details http://www.gnu.org/licenses/gpl.html
 *   DO NOT USE FOR COMMERCIAL WITHOUT PERMISSION
 *
 * GunCMS Version 1.0.3
 * Copyright(c) 2009-2010, T & L GdbR
 * info@guncms.de
 * 
 * http://www.guncms.de
 ********************************************************************************/
class FgcmsMultiLanguageBean {
		
		public function __construct() {
			
		}
		
		public function changelang($txtId) {
			$lang = FgcmsMultiLanguageBean::view_translator_bykey($txtId);
			if(!isset($lang['language_const_id']) || $lang['language_const_id'] == "" )
			{
				$language=array();
				$language['multilanguage']=$txtId;
				FgcmsMultiLanguageBean::save_translator_lang($language);		
			}
			$txtId=$lang['german'];
			$langId = $_SESSION['LC_ALL'];
			$get_lang = FgcmsMultiLanguageBean::getnamelang_bykeylang($langId);
				$txtId=$lang[$get_lang['name_lang']];
				
			return $txtId;
		}
		
		public function get_active_lang($db,$id) {
			$sql = "SELECT * " .
				   "FROM " . TBL_LANGUAGE_SETTING . " s ".
				   " LEFT JOIN " . TBL_LANGUAGE_LIST . " l ON l.lang_id = s.lang_id ".
				   "WHERE s.id = '$id'";
			$result = $db->get_row($sql);
			
			return $result;
			
		}
		
		public function get_lang_byID($id) {
			$sql = "SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "WHERE l.lang_id = '$id'";
			$result = mysql_query($sql);
			
			return mysql_fetch_array($result);
			
		}
		
		public function get_lang_bykey($key) {
			$sql = sprintf("SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "WHERE l.key_lang = '$key'");
			$result = mysql_query($sql);
			
			return mysql_fetch_array($result);
			
		}
		
		public function get_lang_byname($key) {
			$sql = sprintf("SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "WHERE l.name_lang = '$key'");
			$result = mysql_query($sql);
			
			return mysql_fetch_array($result);
			
		}
		
		public function get_all_lang() {
			$data = array();
			$sql = sprintf("SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "ORDER BY l.lang_id ");
			$result = mysql_query($sql);
			if($result !="")
			{
				while ($row = mysql_fetch_array($result)){
					$data[] = $row;
				}
			}
			return $data;
			
		}
		
		
		public function save_translator_lang($lang) {
			$alllang = FgcmsMultiLanguageBean::get_all_lang();
			$str="";
			$strlang="";
			if($alllang!=""){	
				foreach($alllang as $item)
				{
					$name = $item['name_lang'];
					$str .= " ".$name.",";
					$strlang .= " '".$lang[$name]."',";
					$multi = $lang['multilanguage'];
					$email = $lang['for_email'];
				}
			}
			$sql = mysql_query(sprintf("INSERT INTO " . TBL_MULTI_LANGUAGE . 
					" (language_const_id, ".$str." for_email) " .
					"values ('$multi', ".$strlang." '$email')"));
			
		}
		
		
		public function view_translator_bykey($key) {
			$sql = sprintf("SELECT * " .
				   "FROM " . TBL_MULTI_LANGUAGE . " l ".
				   "WHERE l.language_const_id = '$key'");
			$result = mysql_query($sql);
			return mysql_fetch_array($result);
			
		}
		
		
		public function get_list_lang($db) {
			$sql = sprintf("SELECT * FROM " . TBL_LANGUAGE_LIST." l WHERE l.active='1'");
			$result = $db->get_results($sql);
			return $result;
    	}
		
		public function getnamelang_bykeylang($key) {
			$sql = "SELECT * " .
				   "FROM " . TBL_LANGUAGE_LIST . " l ".
				   "WHERE l.key_lang = '$key'";
			$result = mysql_query($sql);
			return mysql_fetch_array($result);
			
		}
	}
	
?>